package com.demo.controller;

import com.demo.pojo.Article;
import com.demo.pojo.ArticleExample;
import com.demo.service.ArticleService;
import com.demo.service.LuceneService;
import com.demo.util.MapUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class ArticleController {
    @Resource
    private ArticleService as;

    @Resource
    private LuceneService ls;

    @RequestMapping ("/get-articles")
    public Map<String,Object> getArticles(@RequestParam(required = false) String content) throws Exception {
        System.out.println("content"+content);
        if(content==null){
            return MapUtil.ok(as.selectByExample(new ArticleExample()));
        }
        List<Article> articleList = ls.search(content);
        if(articleList.isEmpty()){
            return MapUtil.ok(as.search("%"+content+"%"));
        }
        return MapUtil.ok(articleList);
    }

    @RequestMapping("/get-article")
    public Map<String,Object> getArticle(Integer id){
        return MapUtil.ok(as.getArticleWithFullInfo(id));
    }
}
